--Universidade Federal de Campina Grande
--Centro de Enegenharia Elétrica e Informática
--Unidade Acadêmica de Sistemas e Computação
--Disciplina: Paradigmas de Linguagens de Programação
--Professor: Franklin Ramalho
--Equipe: Arthur Ribeiro
--        Felipe Vieira
--        Jeysibel Dantas
--        Paulo Victor
--Euphoric Checkers
--

include board.ex

--first priority: order by goal proximity
--second priority: order by border proximity 
export function chooseBetterMove(sequence board,atom player ,sequence possibleMoves)

    atom goal = 8
    atom goalLeft = 8
    atom goalRight = 1
    sequence preChoosenByHeight = {}
    sequence preChoosenByWidth = {}
    sequence choosed = {}
   --?player
   --?player
   
    if not equal(possibleMoves,{}) then
    
          if equal(player, BLACK) then 
             
            preChoosenByHeight = preChooseByHeight(possibleMoves, goal)
           
            --?preChoosenByHeight
            if not equal(preChoosenByHeight, {}) then
                preChoosenByWidth = preChooseByWidth(preChoosenByHeight,goalLeft,goalRight)
        
            --?preChoosenByWidth
            else
                return {}
            end if
          end if
    else return {}
    end if
    if (length(preChoosenByWidth) > 0) then
      if equal(length(preChoosenByWidth), 1) then
          return preChoosenByWidth[1]
      else 
          atom index = generateIndex(length(preChoosenByWidth))
          --puts(1,"Indice escolhido:")
          --?index
          if isSecureDestination(preChoosenByWidth[index]) then
            return preChoosenByWidth[index]
          else
              index = generateIndex(length(preChoosenByWidth))
              --puts(1,"Indice escolhido Novamente:")
              --?index
                if isSecureDestination(preChoosenByWidth[index]) then
                  return preChoosenByWidth[index]
                else
                  index = generateIndex(length(preChoosenByWidth))
                  --puts(1,"Indice escolhido Novamente:")
                  --?index
                return preChoosenByWidth[index]
          end if
          end if
      end if
    end if
    return {}

end function

function isSecureDestination(sequence choosed)
    atom lineStop = choosed[3]
    atom columnStop = choosed[4]
    if not equal(lineStop,1) and not equal(lineStop,8) and not equal(columnStop,1) and not equal(columnStop,8) then
        if equal(board[lineStop+1][columnStop+1],WHITE ) or equal(board[lineStop+1][columnStop+1],WHITE_KING) then 
          return FALSE
        end if
        if equal(board[lineStop+1][columnStop-1],WHITE ) or equal(board[lineStop+1][columnStop-1],WHITE_KING) then 
          return FALSE
        end if
    end if
return TRUE

end function

function generateIndex(atom range)
    atom random = rand(range)
          integer index = floor(random/2) + 1
return index
end function

function preChooseByHeight(sequence possibleMoves, atom goal)
sequence preChoosenByHeight = {}
--select the goal nearest pieces in increase order

        for i=1 to length(possibleMoves) do
           if equal((goal - possibleMoves[i][3]), 1) then
              preChoosenByHeight = append(preChoosenByHeight, possibleMoves[i])
             -- ?preChoosenByHeight
           end if 
        end for
        for i=1 to length(possibleMoves) do

           if equal(goal - possibleMoves[i][3], 2) then
              preChoosenByHeight = append(preChoosenByHeight, possibleMoves[i])
             -- ?preChoosenByHeight
           end if 
        end for
        for i=1 to length(possibleMoves) do
           if equal(goal - possibleMoves[i][3], 3) then
              preChoosenByHeight = append(preChoosenByHeight, possibleMoves[i])
             -- ?preChoosenByHeight
           end if 
        end for
        --select at least the worth case
--      if equal(preChoosenByHeight,{}) then
            for i=1 to length(possibleMoves) do
              if (goal - possibleMoves[i][3] >= 3) then
                preChoosenByHeight = append(preChoosenByHeight, possibleMoves[i])
              end if 
            end for
--      end if 
return preChoosenByHeight
end function

function preChooseByWidth(sequence preChoosenByHeight, atom goalLeft, atom goalRight)

--?goalLeft
--?goalRight
--?preChoosenByHeight

sequence preChoosenByWidth = {}
      if not equal(preChoosenByHeight,{}) then
          for i=1 to length(preChoosenByHeight) do
            if  equal(goalLeft - preChoosenByHeight[i][4], 0) or equal(goalRight - preChoosenByHeight[i][4], 0) then
                 preChoosenByWidth = append(preChoosenByWidth, preChoosenByHeight[i])
            end if 
          end for
          for i=1 to length(preChoosenByHeight) do
            if equal(goalLeft - preChoosenByHeight[i][4], 1) or equal(goalRight - preChoosenByHeight[i][4], 1) then
                 preChoosenByWidth = append(preChoosenByWidth, preChoosenByHeight[i])
            end if 
          end for
          for i=1 to length(preChoosenByHeight) do
            if equal(goalLeft - preChoosenByHeight[i][4], 2) or equal(goalRight - preChoosenByHeight[i][4],2) then
                 preChoosenByWidth = append(preChoosenByWidth, preChoosenByHeight[i])
            end if 
          end for
          for i=1 to length(preChoosenByHeight) do
            if equal(goalLeft - preChoosenByHeight[i][4], 3) or equal(goalRight - preChoosenByHeight[i][4], 3) then
                 preChoosenByWidth = append(preChoosenByWidth, preChoosenByHeight[i])
            end if 
          end for
          for i=1 to length(preChoosenByHeight) do
            if equal(goalLeft - preChoosenByHeight[i][4], 4) or equal(goalRight - preChoosenByHeight[i][4], 4) then
                 preChoosenByWidth = append(preChoosenByWidth, preChoosenByHeight[i])
            end if 
          end for
      end if 

return preChoosenByWidth

end function

export function chooseBetterTake(sequence board, atom player, sequence requiredTakes)


return {}
end function


export function chooseDeeperTakes(sequence board, atom player, sequence requiredTakes)


return {}
end function

export function isBetterChoice(sequence choice, sequence setOfBetterChoices)
    for i=0 to length(setOfBetterChoices) do
        if equal(choice,setOfBetterChoices[i]) then
            return TRUE
        end if
    end for
return FALSE
end function


export function isGoodChoose(atom lineChoice, atom columnChoice, sequence requiredTakes)
   for i=1 to length(requiredTakes) do
        if lineChoice = requiredTakes[i][1] and columnChoice = requiredTakes[i][2] then
          --takeChoosen = requiredTakes[i]
          return TRUE
          
        end if
    end for

return FALSE

end function
